<template>
  <!-- 当鼠标移出这个home-category容器, 则移除active类 -->
  <div class="home-category" @mouseleave="categoryId = null">
    <ul class="menu">
      <!-- 当鼠标进入到这个li标签内, 将给li绑定active类, 处理之前左侧hover状态分类丢失 -->
      <li v-for="item in menuList"
          :key="item.id"
          :class="{active: categoryId === item.id}"
          @mouseenter="categoryId = item.id">
        <router-link :to="`/category/${item.id}`">
          {{ item.name }}
        </router-link>
        <template v-if="item.children">
          <router-link v-for="sub in item.children"
                       :key="sub.id"
                       :to="`/category/sub/${sub.id}`">
            {{ sub.name }}
          </router-link>
        </template>

        <!-- 骨架, 当分类菜单的二级菜单没有数据会触发 -->
        <template v-else>
          <XtxSkeleton bg="rgba(255, 255, 255, 0.2)" height="18px" style="margin-right: 5px" width="60px"/>
          <XtxSkeleton bg="rgba(255, 255, 255, 0.2)" height="18px" style="margin-right: 5px" width="50px"/>
        </template>
      </li>
    </ul>

    <!-- 弹层 -->
    <div class="layer">
      <h4>{{ categoryName }}推荐 <small>根据您的购买或浏览记录推荐</small></h4>
      <!-- 商品 -->
      <!-- 注意: 商品分类弹层和品牌推荐弹层是对立面, 弹层的结果取决于 curCategory当前分类结果 -->
      <ul v-if="curCategory && curCategory.goods && curCategory.goods.length">
        <li v-for="item in curCategory.goods" :key="item.id">
          <router-link :to="`/product/${item.id}`">
            <img v-lazy="item.picture" alt="">
            <div class="info">
              <p class="name ellipsis-2">{{ item.name }}</p>
              <p class="desc ellipsis">{{ item.desc }}</p>
              <p class="price"><i>¥</i>{{ item.price }}</p>
            </div>
          </router-link>
        </li>
      </ul>
      <!-- 品牌推荐特殊, 需要额外获取数据和额外的布局 -->
      <ul v-if="curCategory && curCategory.brands && curCategory.brands.length">
        <li v-for="item in curCategory.brands"
            :key="item.id"
            class="brand">
          <RouterLink to="/">
            <img v-lazy="item.picture" alt="">
            <div class="info">
              <p class="place"><i class="iconfont icon-dingwei"></i>{{ item.place }}</p>
              <p class="name ellipsis">{{ item.name }}</p>
              <p class="desc ellipsis-2">{{ item.desc }}</p>
            </div>
          </RouterLink>
        </li>
      </ul>
    </div>

  </div>
</template>

<script>
import { useState } from '@/utils/vueMap/mapState'
import { computed, reactive, ref } from 'vue'
import { useStore } from 'vuex'
import API from '@/api/home'
import XtxSkeleton from '@/components/library/XtxSkeleton'

export default {
  name: 'HomeCategory',
  components: { XtxSkeleton },
  setup() {
    const store = useStore()

    // 最终使用的数据 = 9个分类 + 1个品牌
    const brand = reactive({
      id: 'brand',
      name: '品牌',
      children: [{
        id: 'brand-children',
        name: '品牌推荐'
      }],
      // 品牌列表
      brands: []
    })

    // 需要使用解构
    const { list } = useState('category', ['list'])
    // console.log(list)   // {list: ComputedRefImpl}
    // console.log(list.value)   // Proxy {} Array(9) [ {name: '居家'}, {name: '美食'} ]

    // 得到9个分类且每个一级分类下的子分类只有两个
    const menuList = computed(() => {
      const list = store.state.category.list.map(item => {
        return {
          id: item.id,
          name: item.name,
          // 非空判断一下, 防止初始化时没有children的时候调用slice函数报错
          children: item.children && item.children.slice(0, 2),
          goods: item.goods
        }
      })

      list.push(brand)
      return list
    })

    console.log(menuList)

    // 得到弹层的推荐商品数据
    const categoryId = ref(null)
    const curCategory = computed(() => {
      return menuList.value.find(item => item.id === categoryId.value)
    })

    // 分类名称动态判断
    const categoryName = computed(() => {
      return categoryId.value === 'brand' ? '品牌' : '分类'
    })

    console.log(curCategory)

    // 获取品牌数据, 在vue3.0的setup函数中, 尽量不要使用 async / await
    // 且setup里面的都是同步的代码
    API.getHotBrand()
        .then((res) => {
          // 由于menuList和curCategory都是计算属性得到的, 当brand数据变化, 它们都要从新计算
          brand.brands = res.result
        })

    return {
      menuList,
      categoryId,
      categoryName,
      curCategory
    }
  }
}
</script>

<style lang="less" scoped>

.home-category {
  height: 500px;
  width: 250px;
  background-color: rgba(0, 0, 0, .8);

  // z-index 是为了将左侧分类在轮播图的上面
  position: relative;
  z-index: 99;


  .menu {

    li {
      padding-left: 40px;
      // 这个宽度不用手动写, 块级标签的默认宽度占满父级容器的width的100%
      //width: 250px;
      height: 50px;

      &:hover, &.active {
        background-color: @xtxColor;
      }

      a {
        margin-right: 4px;
        color: #fff;
        line-height: 50px;

        &:first-child {
          font-size: 16px;
        }
      }
    }
  }

  .layer {
    position: absolute;
    left: 250px;
    top: 0;

    display: none;
    padding: 0 15px;
    width: 990px;
    height: 500px;
    background: rgba(255, 255, 255, 0.8);

    h4 {
      font-size: 20px;
      font-weight: normal;
      line-height: 80px;

      small {
        font-size: 16px;
        color: #666;
      }
    }

    ul {
      display: flex;
      flex-wrap: wrap;

      li {
        width: 310px;
        height: 120px;
        margin-right: 15px;
        margin-bottom: 15px;
        border: 1px solid #eee;
        border-radius: 4px;
        background-color: #fff;

        // 品牌的样式
        &.brand {
          height: 180px;

          a {
            align-items: flex-start;

            img {
              width: 120px;
              height: 160px;
            }

            .info {
              p {
                margin-top: 8px;
              }

              .place {
                color: #999;
              }
            }
          }
        }


        // 控制最右边的li右边距为0
        &:nth-of-type(3n) {
          margin-right: 0;
        }

        a {
          width: 100%;
          height: 100%;
          display: flex;
          align-items: center;
          padding: 10px;

          &:hover {
            background-color: #e3f9f4;
          }

          img {
            width: 95px;
            height: 95px;
          }

          .info {
            padding-left: 10px;
            line-height: 24px;
            width: 190px;

            .name {
              font-size: 16px;
              color: #666;
            }

            .desc {
              color: #999;
            }

            .price {
              font-size: 22px;
              color: @priceColor;

              i {
                font-size: 16px;
              }
            }
          }
        }
      }
    }
  }

  //
  &:hover {
    .layer {
      display: block;
    }
  }

  .xtx-skeleton {
    animation: fade 1s linear infinite alternate;
  }

  @keyframes fade {
    from {
      opacity: 0.2;
    }
    to {
      opacity: 1;
    }
  }
}
</style>
